<?php
/**
 * IG_Menu_Item
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: Item.php 586 2011-05-31 10:07:18Z mmxie $
 */
class IG_Menu_Item
{
    protected $_id;
    protected $_module;
    protected $_title;
    protected $_color;
    protected $_url;
    protected $_target;

    /**
     * 子菜单
     * @var array
     */
    protected $_submenu;

    /**
     *
     * @param array $options 菜单的默认属性，可使用键值对直接初始化菜单
     */
    public function __construct($options = null)
    {
        if ($options != null) {
            $this->setOptions($options);
        }
    }

    /**
     * 生成html字串
     *
     * @return string
     */
    public function __toString()
    {
        if ($this->_url != '') {
            return '<a href="' . $this->_url . '">' . $this->_title . '</a>';
        } else {
            return $this->_title;
        }
    }

    /**
     * 增加一个子菜单
     *
     * @param IG_Menu_Item $item
     */
    public function addSubmenu($item)
    {
        if (!is_array($this->_submenu)) {
            $this->_submenu = array();
        }
        $this->_submenu[] = $item;
    }

    public function removeSubmenu($item)
    {
        if ($item instanceof IG_Menu_Item) {
            $item = $item->getId();
        }
        foreach ($this->_submenu as $key => $submenu) {
            if ($submenu->getId() == $item) {
                unset($this->_submenu[$key]);
            }
        }
    }

    /**
     * 编辑子菜单
     * @param IG_Menu_Item $item
     */
    public function editSubmenu($item)
    {
        if (!$item instanceof IG_Menu_Item) {
            return;
        }
        foreach ($this->_submenu as $key => $submenu) {
            if ($submenu->getId() == $item->getId()) {
                $this->_submenu[$key] = $item;
            }
        }
    }

    /**
     * 判断当前菜单是否有子菜单
     *
     * @return boolean
     */
    public function hasSubmenu()
    {
        if (!empty($this->_submenu)) {
            return true;
        }
        return false;
    }

    /**
     * 返回子菜单
     *
     * @return array
     */
    public function getSubmenu()
    {
        return $this->_submenu;
    }

    /**
     * 设置子菜单
     *
     * @param array $submenu
     * @return IG_Menu_Item
     */
    public function setSubmenu($submenu)
    {
        $this->_submenu = $submenu;
        return $this;
    }

    /**
     * 设置属性
     *
     * @param array $options
     * @return IG_Menu_Item
     */
    public function setOptions($options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $k => $v) {
            $method = 'set' . ucfirst($k);
            if (in_array($method, $methods)) {
                $this->$method($v);
            }
        }
        return $this;
    }

    /**
     * 获取菜单的ID
     *
     * @return int
     */
    public function getId()
    {
        return $this->_id;
    }

    /**
     * 设置菜单ID
     *
     * @param int $id
     * @return IG_Menu_Item
     */
    public function setId($id)
    {
        $this->_id = $id;
        return $this;
    }

    /**
     * 获取菜单所属模块
     *
     * @return string
     */
    public function getModule()
    {
        return $this->_module;
    }

    /**
     * 设置模块
     *
     * @param string $module
     * @return IG_Menu_Item
     */
    public function setModule($module)
    {
        $this->_module = $module;
        return $this;
    }

    /**
     * 获取菜单标题
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->_title;
    }

    /**
     * 设置菜单标题
     *
     * @param string $title
     * @return IG_Menu_Item
     */
    public function setTitle($title)
    {
        $this->_title = $title;
        return $this;
    }

    /**
     * 获取颜色
     *
     * @return string
     */
    public function getColor()
    {
        return $this->_color;
    }

    /**
     * 设置颜色
     *
     * @param string $color
     * @return IG_Menu_Item
     */
    public function setColor($color)
    {
        $this->_color = $color;
        return $this;
    }

    /**
     * 获取跳转地址
     *
     * @return string
     */
    public function getUrl()
    {
        return $this->_url;
    }

    /**
     * 设置跳转地址
     *
     * @param string $url
     * @return IG_Menu_Item
     */
    public function setUrl($url)
    {
        $this->_url = $url;
        return $this;
    }

    /**
     * 获取打开方式
     *
     * @return string
     */
    public function getTarget()
    {
        return $this->_target;
    }

    /**
     * 设置打开方式
     *
     * @param string $target
     * @return IG_Menu_Item
     */
    public function setTarget($target)
    {
        $this->_target = $target;
        return $this;
    }
}